<template>
  <el-dialog
    title="注册"
    :visible.sync="dialogVisible"
    width="25%"
    :before-close="handleClose"
    :append-to-body="true"
  >
    <el-row>
      <el-form
        label-width="80px"
        ref="createForm"
        :rules="rules"
        :model="formData"
      >
        <el-form-item label="租户" prop="tenantCode">
          <el-input v-model="formData.tenantCode" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="mobileOrEmail">
          <!-- <el-input v-model="formData.adminEmailAddress" /> -->
          <el-input
            placeholder="电子邮箱"
            v-model="formData.mobileOrEmail"
            class="input-with-select"
          >
            <el-button
              slot="append"
              v-if="formData.mobileOrEmail.length > 0"
              @click="send()"
              icon="el-icon-refresh"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item
          label="新密码"
          prop="password"
          v-if="formData.type == 'Reset'"
        >
          <el-input v-model="formData.password" />
        </el-form-item>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="comit()"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
// import { CreateOrUpdate } from '@api/systemManage/menuManage'
import { SendEmail, CheckCode } from '@api/systemManage/tenantManage'
export default {
  data () {
    return {
      title: '',
      dialogVisible: false,
      loading: false,
      type: '',
      formData: {
        tenantCode: '',
        mobileOrEmail: '',
        password: '',
        code: '',
        type: 'Reset'
      },
      rules: {
        mobileOrEmail: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        tenantCode: [
          { required: true, message: '请输入租户代码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入租户代码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    comit () {
      this.$refs.createForm.validate((valid) => {
        if (valid) {
          this.loading = true
          CheckCode(this.formData).then(res => {
            this.loading = false
            if (res.success) {
              if (this.formData.type === 'Tenant') {
                this.$message({
                  message: '租户激活成功,账号密码已下发邮件，您可以在登录界面点击[切换租户]进行租户登录',
                  type: 'success'
                })
              } else {
                this.$message({
                  message: '密码重置成功，新密码已通过下发邮件，请注意查收',
                  type: 'success'
                })
              }
              this.handleClose()
            } else {
              this.$message.error(res.error.message)
            }
          })
        }
      })
    },
    send () {
      var model = {
        id: this.formData.mobileOrEmail
      }
      SendEmail(model).then(res => {
        if (res.success) {
          this.$message({
            message: '验证码已发送，请注意查收！',
            type: 'success'
          })
        }
      })
    },
    show (obj) {
      this.formData = obj
      if (this.formData.type === 'Tenant' && this.formData.mobileOrEmail !== '') {
        this.send()
      }

      this.dialogVisible = true
    },
    handleClose () {
      this.formData = {
        tenantCode: '',
        mobileOrEmail: '',
        password: '',
        code: '',
        type: 'Reset'
      }
      this.dialogVisible = false
    }
  }
}
</script>
